﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace TowerDefense.Units
{
    public static class CreepFactory
    {
        public static Creep CreateCreep(CreepTypes type, int levelModifer)
        {
            Creep c = new Creep(World.CurrentMap.Route.Waypoints,World.CurrentMap.EndPoint,World.CurrentMap.StartingPoint,false);

            switch (type)
            {
                case CreepTypes.GroundCreep:
                    c.Armor = 1;
                    c.Health = 100;
                    c.StartHealth = 100;
                    c.CurrentState = Creep.CreepState.Moving;
                    c.IsAir = false;
                    c.Name = "Level " + levelModifer.ToString() + " ground Creep";
                    c.BaseSpeed = 0.05;
                    c.Texture = TDGame.textures["pacman"];
                    c.TopView = false;
                    break;
            }

            return c;
        }

        public static List<Creep> CreateLots(CreepTypes type, int levelModifier, int amount)
        {
            List<Creep> creeps = new List<Creep>(amount);

            Creep c = CreateCreep(type, levelModifier);

            for (int i = 0; i <= amount; i++)
            {
                creeps.Add((Creep)c.Clone());
            }

            return creeps;
        }

    }

    public static class CreepSpawner
    {
        private static Queue<Creep> _creeps = new Queue<Creep>();

        private static TimeSpan _lastSpawn;

        private static TimeSpan timeElapsedSinceLastSpawn;

        public static Queue<Creep> Creeps
        {
            get
            {
                return _creeps;
            }
            set
            {
                _creeps = value;
            }
        }

        public static void Update(GameTime gameTime)
        {
            timeElapsedSinceLastSpawn = gameTime.TotalGameTime - _lastSpawn;

            if (timeElapsedSinceLastSpawn.Milliseconds > 800 && Creeps.Count > 0)
            {
                World.Creeps.Add(Creeps.Dequeue());
                _lastSpawn = gameTime.TotalGameTime;
            }
        }
    }
}